package com.example.housingRentalSystem.controller;

import com.example.housingRentalSystem.bean.Feedback;
import com.example.housingRentalSystem.service.FeedbackService;
import com.example.housingRentalSystem.utils.ApiResponse;
import com.example.housingRentalSystem.utils.HttpStatus;
import com.example.housingRentalSystem.utils.PageRequest;
import com.example.housingRentalSystem.utils.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author 王硕
 * @version jdk21
 * @className FeedbackController
 * @description
 * @date 2025/3/24 10:45
 */
@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;

    @PostMapping("/createFeedback/{userId}")
    public ApiResponse<Void> createFeedback(@Valid @RequestBody Feedback feedback,
                                            @PathVariable Integer userId) {
        feedbackService.createFeedback(feedback, userId);
        return ApiResponse.success(HttpStatus.OK.getCode(), "反馈提交成功");
    }

    @PostMapping("/replyFeedback/{id}")
    public ApiResponse<Void> replyFeedback(@PathVariable Integer id,
                                           @RequestBody Map<String, String> replyData) {
        String reply = replyData.get("reply");
        feedbackService.replyFeedback(id, reply);
        return ApiResponse.success(HttpStatus.OK.getCode(), "回复成功");
    }

    @DeleteMapping("/deleteFeedback/{id}")
    public ApiResponse<Void> deleteFeedback(@PathVariable Integer id,
                                            @RequestParam Integer userId) {
        feedbackService.deleteFeedback(id, userId);
        return ApiResponse.success(HttpStatus.OK.getCode(), "删除成功");
    }

    @GetMapping("/listFeedbacks")
    public ApiResponse<PageResponse<Feedback>> listFeedbacks(
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer userId,
            @ModelAttribute PageRequest pageRequest) {
        return ApiResponse.success(feedbackService.getFeedbackList(status, userId, pageRequest));
    }

    @GetMapping("/selectCountFeedback")
    public ApiResponse<Integer> selectCountFeedback() {
        return ApiResponse.success(feedbackService.selectCountFeedback());
    }

}